import router from '@/router'
import store from '@/store'

/**
 * 标签管理，用于关闭标签
 * @param {*} Vue
 * @param {*} options
 */
export default function(Vue, options) {
  function toLastView(visitedViews, view) {
    const latestView = visitedViews.slice(-1)[0]
    if (latestView) {
      router.push(latestView.fullPath)
    } else {
      // now the default is to redirect to the home page if there is no tags-view,
      // you can adjust it according to your needs.
      if (view.name === 'Dashboard') {
        // to reload home page
        router.replace({ path: '/redirect' + view.fullPath })
      } else {
        router.push('/')
      }
    }
  }
  Vue.prototype.$closeTag = function(view, openView, justClose) {
    store.dispatch('tagsView/delView', view).then(({ visitedViews }) => {
      if (justClose) {
        return
      }
      // isActive(route) {
      //   return route.path === this.$route.path
      // },
      // if (this.isActive(this.$route)) {
      if (!openView) {
        toLastView(visitedViews, view)
        // toLastView(visitedViews, this.$route)
      } else {
        router.push(openView)
      }
      // }
    })
  }
}
